<template>
<!-- 添加配置 -->
  <div>
    <el-dialog
      title="添加用户推荐配置"
      :visible.sync="isShow"
      width="40%"
      center
      :before-close="handleClose">
      <el-form :model="from" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="配置名称：" prop="name">
          <el-input v-model="from.name"></el-input>
        </el-form-item>
        <el-form-item label="兑换比例：" prop="value">
          <el-input v-model="from.value"></el-input>
        </el-form-item>
        <!-- <el-form-item label="推荐类型" prop="type">
          <el-select v-model="from.type" placeholder="请选择推荐类型">
            <el-option label="发明专利" value="F"></el-option>
            <el-option label="实用新型" value="S"></el-option>
            <el-option label="外观设计" value="W"></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" v-no-more-click:3000 @click="submit('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {

  data () {
    var validateMoney = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入兑换比例'));
      } else {
        if (!Number(value)) {
          callback(new Error('请输入正确的兑换比例'));
        } else if (value <= 0) {
          callback(new Error('兑换比例必须大于0'));
        } else {
          callback()
        }
      }

    }
    return {
      isShow: false,
      from: {
        value: '',
        type: '',
        name: ''
      },
      rules: {
        type: [
          { required: true, message: '请选择推荐类型', trigger: 'change' }
        ],
        value: [
          { validator: validateMoney, trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入配置名称', trigger: 'change' }
        ],
      },
      category: ''
    }
  },
  methods: {
    show (data) {
      this.isShow = true
      this.category = data.key
      // this.$refs.ruleForm.resetFields()
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if(valid) {
          this.submitPost()
        }
      })
    },
    submitPost() {
      let params = {
        key: 'distribution_ratio',
        value: this.from.value,
        name: this.from.name,
        category: this.category
      }
      this.$axios.post('api/config_setting', params).then(res => {
        if (res.data.code == 200) {
          this.$message({
            message: '添加推荐配置成功！',
            type: 'success'
          })
          this.$emit('getData')
          this.isShow = false
        } else if (res.data.code == 400) {
          this.$message.error(res.data.message)
        }

      })
    },
    handleClose () {
      this.isShow = false
      this.from = {
        money: '',
        type: '',
        name: ''
      }
      this.$refs.ruleForm.resetFields();
    }
  }
}
</script>

<style lang='less' type='text/css'>
  .el-select {
    width: 100%;
  }
</style>
